package cn.t.repository.impl.user;

import cn.t.converter.user.RoleSkuConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.RoleSkuDO;
import cn.t.persistence.entity.user.RoleSkuPO;
import cn.t.persistence.mapper.user.RoleSkuMapper;
import cn.t.repository.user.RoleSkuRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门信息表数据仓库
 *
 * @author l
 */
@Slf4j
@Repository
@AllArgsConstructor
public class RoleSkuRepositoryImpl extends RepositoryImpl<RoleSkuMapper, RoleSkuPO, RoleSkuConverter, RoleSkuDO> implements RoleSkuRepository {


    @Override
    public List<RoleSkuDO> findByModel(RoleSkuDO roleSkuDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<RoleSkuPO> list = getWrapper(roleSkuDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<RoleSkuPO> getWrapper(RoleSkuDO roleSkuDO) {
        LambdaQueryChainWrapper<RoleSkuPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(roleSkuDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(roleSkuDO.getGmtCreatedEnd())) {
            wrapper.between(RoleSkuPO::getGmtCreated, roleSkuDO.getGmtCreatedStart(), roleSkuDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(roleSkuDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(roleSkuDO.getGmtModifiedEnd())) {
            wrapper.between(RoleSkuPO::getGmtModified, roleSkuDO.getGmtModifiedStart(), roleSkuDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(roleSkuDO.getSkuCode())) {
            wrapper.eq(RoleSkuPO::getSkuCode, roleSkuDO.getSkuCode());
        }
        if (!ObjectUtils.isEmpty(roleSkuDO.getRoleId())) {
            wrapper.eq(RoleSkuPO::getRoleId, roleSkuDO.getRoleId());
        }

        if (!ObjectUtils.isEmpty(roleSkuDO.getStatus())) {
            wrapper.eq(RoleSkuPO::getStatus, roleSkuDO.getStatus());
        } else {
            wrapper.eq(RoleSkuPO::getStatus, BaseStatusEnum.AVAILABLE.getCode());
        }
        if (!ObjectUtils.isEmpty(roleSkuDO.getIsDeleted())) {
            wrapper.eq(RoleSkuPO::getIsDeleted, roleSkuDO.getIsDeleted());
        } else {
            wrapper.eq(RoleSkuPO::getIsDeleted, YesNoEnum.FALSE.getCode());
        }

        return wrapper;
    }

    @Override
    public long count(RoleSkuDO roleSkuDO) {
        return getWrapper(roleSkuDO).count();
    }

    @Override
    public boolean removeByRoleId(Long roleId) {
        return getBaseMapper().removeByRoleId(roleId);
    }

    @Override
    public boolean removeById(Long id) {
        return getBaseMapper().removeById(id);
    }
}
